part of '../../repository.dart';

const _kWebPurchasedSubscriptionAutoRenewalEnabledKey = 'auto_renewal_enabled';
const _kWebPurchasedSubscriptionOriginalTransactionIdKey = 'original_transaction_id';
const _kWebPurchasedSubscriptionStatusKey = 'status';
const _kWebPurchasedSubscriptionExpirationTimeKey = 'expiration_time';
const _kWebPurchasedSubscriptionTransactionsKey = 'transactions';

const _kWebPurchasedSubscriptionTypeValue = 'purchased_subscription';

class _WebPurchasedSubscriptionDto extends Equatable implements _WebPurchasedItemDto {
  const _WebPurchasedSubscriptionDto._internal({
    required this.purchaseItemSku,
    required this.originalTransactionId,
    required this.autoRenewalEnabled,
    required this.statusDto,
    required this.expirationTimeDto,
    required this.transactionDtos,
  });

  @override
  final String purchaseItemSku;

  final String originalTransactionId;
  final bool autoRenewalEnabled;
  final _WebPurchasedSubscriptionStatusDto statusDto;
  final ApiTimeDto? expirationTimeDto;
  final BuiltList<_WebPurchasedSubscriptionTransactionDto> transactionDtos;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      originalTransactionId,
      autoRenewalEnabled,
      statusDto,
      expirationTimeDto,
      transactionDtos,
    ];
  }

  factory _WebPurchasedSubscriptionDto.fromJson(Map<String, dynamic> json) {
    final statusString = json[_kWebPurchasedSubscriptionStatusKey] as String;
    final statusDto = _WebPurchasedSubscriptionStatusDto.fromString(statusString);

    final expirationTimeString = json[_kWebPurchasedSubscriptionExpirationTimeKey] as String?;

    ApiTimeDto? expirationTimeDto;

    if (expirationTimeString != null) {
      expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);
    }

    final transactionMaps =
        List.castFrom<dynamic, Map>(json[_kWebPurchasedSubscriptionTransactionsKey]);

    final transactionDtos = transactionMaps.map((transactionMap) {
      final transactionJson = Map.castFrom<dynamic, dynamic, String, dynamic>(transactionMap);
      return _WebPurchasedSubscriptionTransactionDtoFactory().fromJson(transactionJson);
    }).toBuiltList();

    return _WebPurchasedSubscriptionDto._internal(
      purchaseItemSku: json[_kWebPurchasedItemPurchaseItemSkuKey],
      originalTransactionId: json[_kWebPurchasedSubscriptionOriginalTransactionIdKey],
      autoRenewalEnabled: json[_kWebPurchasedSubscriptionAutoRenewalEnabledKey],
      statusDto: statusDto,
      expirationTimeDto: expirationTimeDto,
      transactionDtos: transactionDtos,
    );
  }

  @override
  WebPurchasedSubscription toEntity() {
    final transactions = transactionDtos.map((transactionDto) {
      return transactionDto.toEntity();
    }).toBuiltList();

    return WebPurchasedSubscription(
      originalTransactionId: originalTransactionId,
      purchaseItemSku: purchaseItemSku,
      autoRenewalEnabled: autoRenewalEnabled,
      status: statusDto.toEntity(),
      expirationTime: expirationTimeDto?.toEntity(),
      transactions: transactions,
    );
  }
}
